﻿/**
 * Copyright © TnT-VNGroup 2011
 * PROJECT:   SmartCollector
 * $HeadURL: https://smartcollector.googlecode.com/svn/branches/Srcs/SmartCollector.HtmlParser/Utilities/TagHelper.cs $
 * $Author: chithanh12@gmail.com $
 * $Date: 2011-04-20 11:46:52 -0700 (Wed, 20 Apr 2011) $
 * $Rev: 94 $
 **/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartCollector.Domain.Entity;
using System.Data.Linq;
using SmartCollector.Domain.Exceptions;

namespace SmartCollector.Domain.Repository
{
    public class LinkRssRepository
    {
        private Table<tbl_LinkRSS> m_linkRssTables;

        public IQueryable<tbl_LinkRSS> LinkRsses
        {
            get
            {
                return m_linkRssTables.AsQueryable();
            }
        }

        public LinkRssRepository()
        {
            m_linkRssTables = (new EntityDomainDataContext()).GetTable<tbl_LinkRSS>();
        }

        public void RemoveLinkRss(int siteId, int categoryId)
        {
            tbl_LinkRSS deletedLinkRss = m_linkRssTables
                .Where(p => p.SiteReferenceId == siteId && p.CategoryId == categoryId)
                .First();
            if (deletedLinkRss != null)
            {
                m_linkRssTables.DeleteOnSubmit(deletedLinkRss);
            }
        }

        public void RemoveLinkRss(int categoryId)
        {
            var deletedLinkRsses = m_linkRssTables
                .Where(p => p.CategoryId == categoryId);
            if (deletedLinkRsses != null)
            {
                m_linkRssTables.DeleteAllOnSubmit(deletedLinkRsses);
            }
        }

        public void AddNewLinkRss(string LinkRss, int siteId, int categoryId)
        {
            tbl_LinkRSS link =m_linkRssTables
                .Where(p => p.SiteReferenceId == siteId && p.CategoryId == categoryId)
                .FirstOrDefault();
            if (link !=null)
            {
                throw new LinkRssAlreadyExistException(link.tbl_ReferenceSite.SiteName,
                    link.tbl_Category.CategoryName);
            }

            m_linkRssTables.InsertOnSubmit(
                new tbl_LinkRSS
                {
                     CategoryId = categoryId,
                     SiteReferenceId = siteId,
                      LinkRSS = LinkRss
                });
        }

        public void SubmitChanged()
        {
            m_linkRssTables.Context.SubmitChanges();
        }
    }
}
